package com.lengxf.pay.web.service;

import cn.hutool.core.lang.Assert;
import com.lengxf.pay.common.bean.PayOrder;
import com.lengxf.pay.common.bean.QueryOrder;
import com.lengxf.pay.common.bean.RefundOrder;
import com.lengxf.pay.common.bean.RefundOrderQuery;
import com.lengxf.pay.starter.parent.bean.PayBackResult;
import com.lengxf.pay.starter.parent.bean.QueryResult;
import com.lengxf.pay.starter.parent.bean.RefundQueryResult;
import com.lengxf.pay.starter.parent.bean.RefundResult;
import com.lengxf.pay.starter.parent.pay.WebBasePayService;
import com.lengxf.pay.web.entity.OrderInfo;
import com.lengxf.pay.web.entity.RefundOrderInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BizPayService {

    private final WebBasePayService webBasePayService;
    private final OrderService orderService;
    private final RefundOrderService refundOrderService;

    public BizPayService(WebBasePayService webBasePayService,
                         @Autowired(required = false) OrderService orderService,
                         @Autowired(required = false) RefundOrderService refundOrderService) {
        Assert.notNull(orderService);
        Assert.notNull(refundOrderService);
        this.webBasePayService = webBasePayService;
        this.orderService = orderService;
        this.refundOrderService = refundOrderService;
    }

    public String toPay(String tenantryId, PayOrder payOrder) {
        //这个逻辑可以考虑多久之后 关闭订单
        orderService.saveOrder(tenantryId, payOrder);
        return webBasePayService.toPay(tenantryId, payOrder);
    }


    public RefundResult refund(RefundOrder refundOrder) {
        OrderInfo orderInfo = orderService.selectOrderByOrderNo(refundOrder.getOrderNo());
        orderInfo.refundCheck();
        String refundOrderNo = refundOrderService.saveRefundOrder(orderInfo, refundOrder);
        RefundResult refundResult = webBasePayService.refund(refundOrder, orderInfo.getTenantryId());
        refundOrderService.updateRefundOrder(refundOrderNo, refundResult);
        return refundResult;
    }

    public void payBack(HttpServletRequest request, String tenantryId) {
        PayBackResult payBackResult = webBasePayService.payBack(request, tenantryId);
        orderService.updateOrder(payBackResult);
    }

    public QueryResult syncOrder(QueryOrder queryOrder) {
        OrderInfo orderInfo = orderService.selectOrderByOrderNo(queryOrder.getOrderNo());
        orderInfo.syncOrderCheck();
        QueryResult query = webBasePayService.query(queryOrder, orderInfo.getTenantryId());
        orderService.updateOrder(query);
        return query;
    }

    public RefundQueryResult syncRefundOrder(RefundOrderQuery refundOrderQuery) {
        RefundOrderInfo refundOrderInfo = refundOrderService.selectByRefundOrderNo(refundOrderQuery.getRefundOrderNo());
        refundOrderInfo.syncRefundOrderCheck();
        RefundQueryResult refundQueryResult = webBasePayService.refundQuery(refundOrderQuery, refundOrderInfo.getTenantryId());
        refundOrderService.updateRefundOrder(refundOrderQuery.getRefundOrderNo(), refundQueryResult);
        return refundQueryResult;
    }

}
